package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.me.ui.RefuseReasonActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatBean;
import com.uchengwang.ui.cm_ui.umsg.http.ChatHttpManager;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2017/12/15.
 * 审核享聊的界面
 */

public class ExamineWantChatActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private ImageView head_bigImg, head_1, head_2;
    private RelativeLayout name_rl, level_rl;
    private TextView name, levelTv, noPass, schoolTv, signTv;
    private ImageView sexImg, level_img;
    private LinearLayout agree_ln, refuse_ln, examine_ln2, sign_rl;//通过 拒绝
    private TextView divider, examine_agree, examine_refuse, examine_noData;//分割线

    private int examineType;//-1=未通过 0=待审核 1=已通过
    private ImageView examine_agree_icon;
    private WantChatBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_want_examine);
        examineType = getIntent().getIntExtra(BaseConfig.TYPE, 0);
        bean = (WantChatBean) getIntent().getSerializableExtra(BaseConfig.BEAN);
        setTitle("审核");
        initView();

    }

    private void initView() {
        head_bigImg = (ImageView) findViewById(R.id.wantMe_head_bigImg);//三个头像
        head_1 = (ImageView) findViewById(R.id.wantMe_head_1);
        head_2 = (ImageView) findViewById(R.id.wantMe_head_2);

        name_rl = (RelativeLayout) findViewById(R.id.wantMe_name_rl);
        name = (TextView) findViewById(R.id.wantMe_name);
        sexImg = (ImageView) findViewById(R.id.wantMe_sex);

        level_rl = (RelativeLayout) findViewById(R.id.wantMe_level_rl);//魅力值显示
        levelTv = (TextView) findViewById(R.id.wantMe_level);
        level_img = (ImageView) findViewById(R.id.wantMe_level_img);
        noPass = (TextView) findViewById(R.id.wantMe_noPass);

        schoolTv = (TextView) findViewById(R.id.wantMe_school);

        sign_rl = (LinearLayout) findViewById(R.id.wantMe_sign_rl);
        signTv = (TextView) findViewById(R.id.wantMe_sign);


//        examine_ln2 = (LinearLayout) findViewById(R.id.examine_ln2);
        agree_ln = (LinearLayout) findViewById(R.id.examine_agree_ln);
        agree_ln.setOnClickListener(this);
        refuse_ln = (LinearLayout) findViewById(R.id.examine_refuse_ln);
        refuse_ln.setOnClickListener(this);
        divider = (TextView) findViewById(R.id.examine_divider);

        examine_agree = (TextView) findViewById(R.id.examine_agree);
        examine_refuse = (TextView) findViewById(R.id.examine_refuse);
//        examine_noData = (TextView) findViewById(R.id.examine_noData);

        examine_agree_icon = (ImageView) findViewById(R.id.examine_agree_icon);

        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);

        initData();
    }

    private void initData(){
        if (bean != null){
            name.setText(bean.getChatName());
            int sex = bean.getSex();//0=女 1=男
            if (sex ==0){
                ImageManager.getInstance().loadImage(this, sexImg, R.drawable.icon_want_chat_woman, ImageManager.ImageType.NARMOL);
            }else {
                ImageManager.getInstance().loadImage(this, sexImg, R.drawable.icon_want_chat_man, ImageManager.ImageType.NARMOL);
            }

            examineType = bean.getIsExamine();

            if (examineType != 0){
                name_rl.setOnClickListener(this);
                sign_rl.setOnClickListener(this);
            }

            schoolTv.setText(bean.getUniName());
            signTv.setText(bean.getChatSign());

            switch (examineType){//-1=未通过 0=待审核 1=通过
                case -1:
                    levelTv.setText("未通过");
                    levelTv.setVisibility(View.VISIBLE);
                    break;
                case 0:
                    levelTv.setVisibility(View.VISIBLE);
                    levelTv.setText("审核中");
                    break;
                case 1:
                    level_img.setVisibility(View.VISIBLE);
                    levelTv.setText("1");
                    break;
            }


            List<String> imgs = bean.getHeads();
            if (imgs != null && imgs.size() >0){
                for (int i=0; i<imgs.size(); i++){
                    String url = imgs.get(i);
                    switch (i){
                        case 0:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, url, ImageManager.ImageType.ROUND);
                                head_bigImg.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                        case 1:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, url, ImageManager.ImageType.ROUND);
                                head_1.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                        case 2:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, url, ImageManager.ImageType.ROUND);
                                head_2.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                    }

                }
            }

            changeStatus();
        }

    }

    private void changeStatus(){
        switch (examineType){
            case 0://-1=未通过 0=待审核 1=已通过
                break;
            case 1:
                refuse_ln.setVisibility(View.GONE);
                divider.setVisibility(View.GONE);
                examine_agree.setText("已通过");
                agree_ln.setEnabled(false);
                break;
            case -1:
                agree_ln.setVisibility(View.GONE);
                divider.setVisibility(View.GONE);
                examine_refuse.setText("已拒绝");
                refuse_ln.setEnabled(false);
                break;
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.wantMe_head_bigImg:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 0, false);
                break;
            case R.id.wantMe_head_1:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 1, false);
                break;
            case R.id.wantMe_head_2:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 2, false);
                break;
            case R.id.examine_agree_ln://通过
                examine(true);
                break;
            case R.id.examine_refuse_ln://拒绝
                Intent intent = new Intent(this, RefuseReasonActivity.class);
                intent.putExtra(BaseConfig.BEAN, bean);
                intent.putExtra(BaseConfig.TYPE, 1);
                startActivityForResult(intent, 1);
                break;
        }
    }

    //是否拒绝掉
    private void examine(final boolean isPass){
        if (bean == null){
            return;
        }
        final String reason = isPass ? "" : "拒绝";
        DialogUtils.getUtils().showDialogSingle(this, "确定通过该享聊?", "确定", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.getUtils().cancel();
                ChatHttpManager.getManager().wantExamine(ExamineWantChatActivity.this, 1, bean.getUid(), isPass, reason, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            if (isPass){
                                examineType = 1;//通过
                            }else {
                                examineType = -1;//拒绝
                            }
                            changeStatus();

                            bean.setIsExamine(examineType);
                            EventBus.getDefault().post(bean);
                        }else {
                            UCommonUtil.getUtil().showToast(ExamineWantChatActivity.this, msg);
                        }
                    }
                });
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null){
            switch (resultCode){
                case 1:
                    examineType = data.getIntExtra(BaseConfig.IS_EXAMINE, -1);
                    break;
            }

            changeStatus();
        }
    }

}
